/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package gui;

import dao.*;
import java.util.List;
import javax.swing.DefaultComboBoxModel;
import javax.swing.JOptionPane;
import javax.swing.table.DefaultTableModel;
import org.hibernate.HibernateException;
import pojo.*;

/**
 *
 * @author HoaDinhGiang
 */
public class ThongKeDoanhThuInternalFrame extends javax.swing.JInternalFrame {

    /**
     * Creates new form ThongKeDoanhThuInternalFrame
     */
    private List<Phieuxuathang> dsPhieuXuat;
    private DefaultTableModel dtm;
    private double _tongDoanhThu;

    public ThongKeDoanhThuInternalFrame() {
        initComponents();
        dtm = new DefaultTableModel();
        dtm.addColumn("Mã");
        dtm.addColumn("Nhân Viên");
        dtm.addColumn("Cửa Hàng");
        dtm.addColumn("Hình Thức Thanh Toán");
        dtm.addColumn("Điều Kiện Thanh Toán");
        dtm.addColumn("Trạng Thái Đơn Hàng");
        dtm.addColumn("Kho Hàng");
        dtm.addColumn("Ngày Tháng");
        dtm.addColumn("Tổng Tiền");
    }

    public void hienThiTatCaDoanhThu() {

        jtbPhieuXuatHang.removeAll();
        dsPhieuXuat = phieuxuatDAO.layDanhSachPhieuXuatTonTai();
        //Tạo model cho Table


        dtm.setNumRows(dsPhieuXuat.size());
        this._tongDoanhThu = 0;
        for (int i = 0; i < dsPhieuXuat.size(); i++) {
            Phieuxuathang hh = dsPhieuXuat.get(i);
            dtm.setValueAt(hh.getMaphieuxuathang(), i, 0);

            dtm.setValueAt(hh.getNhanvien().getTennhanvien(), i, 1);
            dtm.setValueAt(hh.getCuahang().getTencuahang(), i, 2);
            dtm.setValueAt(hh.getHinhthucthanhtoan().getTenhinhthucthanhtoan(), i, 3);
            dtm.setValueAt(hh.getDieukienthanhtoan().getTendieukienthanhtoan(), i, 4);
            dtm.setValueAt(hh.getTrangthaidonhang().getTentrangthaidonhang(), i, 5);
            dtm.setValueAt(hh.getKhohang().getTenkhohang(), i, 6);
            dtm.setValueAt(hh.getNgaythang(), i, 7);
            dtm.setValueAt(hh.getTongtien(), i, 8);
            this._tongDoanhThu += hh.getTongtien();
            
        }
        this.jlbTongTienXuat.setText(String.valueOf(_tongDoanhThu));
        //Hiển thị lên Table
        this.jtbPhieuXuatHang.setModel(dtm);
        this.jtbPhieuXuatHang.changeSelection(0, 0, false, false);

    }

    public void hienThiTatCaDoanhTheoThang() {

        jtbPhieuXuatHang.removeAll();
        dsPhieuXuat = phieuxuatDAO.layDanhSachPhieuXuatTonTai();

        //Tạo model cho Table


        dtm.setNumRows(dsPhieuXuat.size());
        this._tongDoanhThu = 0;
        for (int i = 0; i < dsPhieuXuat.size(); i++) {
            Phieuxuathang hh = dsPhieuXuat.get(i);
           if (hh.getNgaythang().before(jdcngayketthuc.getDate()) && hh.getNgaythang().after(jdcngaybatdau.getDate())) {
                dtm.setValueAt(hh.getMaphieuxuathang(), i, 0);

                dtm.setValueAt(hh.getNhanvien().getTennhanvien(), i, 1);
                dtm.setValueAt(hh.getCuahang().getTencuahang(), i, 2);
                dtm.setValueAt(hh.getHinhthucthanhtoan().getTenhinhthucthanhtoan(), i, 3);
                dtm.setValueAt(hh.getDieukienthanhtoan().getTendieukienthanhtoan(), i, 4);
                dtm.setValueAt(hh.getTrangthaidonhang().getTentrangthaidonhang(), i, 5);
                dtm.setValueAt(hh.getKhohang().getTenkhohang(), i, 6);
                dtm.setValueAt(hh.getNgaythang(), i, 7);
                dtm.setValueAt(hh.getTongtien(), i, 8);
                this._tongDoanhThu += hh.getTongtien();
                
            }
        }
        //Hiển thị lên Table
        this.jlbTongTienXuat.setText(String.valueOf(_tongDoanhThu));
        this.jtbPhieuXuatHang.setModel(dtm);
        this.jtbPhieuXuatHang.changeSelection(0, 0, false, false);

    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jLabel1 = new javax.swing.JLabel();
        jPanel1 = new javax.swing.JPanel();
        jScrollPane1 = new javax.swing.JScrollPane();
        jtbPhieuXuatHang = new javax.swing.JTable();
        jbtThongKe = new javax.swing.JButton();
        jbtThoat = new javax.swing.JButton();
        jLabel2 = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();
        jlbTongTienXuat = new javax.swing.JLabel();
        jdcngaybatdau = new com.toedter.calendar.JDateChooser();
        jdcngayketthuc = new com.toedter.calendar.JDateChooser();
        jbtToanBoDoanhThu = new javax.swing.JButton();

        setIconifiable(true);
        setMaximizable(true);
        setResizable(true);
        setTitle("Thống Kê Xuất Hàng");
        addInternalFrameListener(new javax.swing.event.InternalFrameListener() {
            public void internalFrameClosed(javax.swing.event.InternalFrameEvent evt) {
            }
            public void internalFrameClosing(javax.swing.event.InternalFrameEvent evt) {
            }
            public void internalFrameOpened(javax.swing.event.InternalFrameEvent evt) {
                formInternalFrameOpened(evt);
            }
            public void internalFrameActivated(javax.swing.event.InternalFrameEvent evt) {
            }
            public void internalFrameDeactivated(javax.swing.event.InternalFrameEvent evt) {
            }
            public void internalFrameDeiconified(javax.swing.event.InternalFrameEvent evt) {
            }
            public void internalFrameIconified(javax.swing.event.InternalFrameEvent evt) {
            }
        });

        jLabel1.setText("Từ Tháng:");

        jPanel1.setBorder(javax.swing.BorderFactory.createTitledBorder("Thống Kê Xuất Hàng"));

        jtbPhieuXuatHang.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {},
                {},
                {},
                {}
            },
            new String [] {

            }
        ));
        jScrollPane1.setViewportView(jtbPhieuXuatHang);

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jScrollPane1)
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jScrollPane1, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, 243, Short.MAX_VALUE)
        );

        jbtThongKe.setText("Thống Kê");
        jbtThongKe.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jbtThongKeActionPerformed(evt);
            }
        });

        jbtThoat.setText("Thoát");
        jbtThoat.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jbtThoatActionPerformed(evt);
            }
        });

        jLabel2.setText("Đến Tháng:");

        jLabel3.setText("Tổng Tiền Xuất Hàng:");

        jlbTongTienXuat.setFont(new java.awt.Font("Tahoma", 1, 14)); // NOI18N

        jdcngaybatdau.setDateFormatString("dd/MM/yyyy");

        jdcngayketthuc.setDateFormatString("dd/MM/yyyy");

        jbtToanBoDoanhThu.setText("Toàn Bộ Doanh Thu");
        jbtToanBoDoanhThu.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jbtToanBoDoanhThuActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addGroup(layout.createSequentialGroup()
                        .addGap(10, 10, 10)
                        .addComponent(jLabel3)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jlbTongTienXuat, javax.swing.GroupLayout.PREFERRED_SIZE, 130, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 231, Short.MAX_VALUE)
                        .addComponent(jbtThongKe, javax.swing.GroupLayout.PREFERRED_SIZE, 116, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(44, 44, 44)
                        .addComponent(jbtThoat)
                        .addContainerGap())))
            .addGroup(layout.createSequentialGroup()
                .addGap(19, 19, 19)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jLabel2)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jdcngayketthuc, javax.swing.GroupLayout.PREFERRED_SIZE, 212, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addComponent(jbtToanBoDoanhThu, javax.swing.GroupLayout.PREFERRED_SIZE, 168, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addContainerGap())
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jLabel1)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(jdcngaybatdau, javax.swing.GroupLayout.PREFERRED_SIZE, 212, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(0, 0, Short.MAX_VALUE))))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(17, 17, 17)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                            .addComponent(jdcngaybatdau, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(jLabel1))
                        .addGap(18, 18, 18)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                            .addComponent(jLabel2)
                            .addComponent(jdcngayketthuc, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
                    .addComponent(jbtToanBoDoanhThu, javax.swing.GroupLayout.PREFERRED_SIZE, 31, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(17, 17, 17)
                .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                        .addComponent(jbtThongKe, javax.swing.GroupLayout.PREFERRED_SIZE, 32, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGroup(layout.createSequentialGroup()
                            .addComponent(jbtThoat, javax.swing.GroupLayout.DEFAULT_SIZE, 33, Short.MAX_VALUE)
                            .addContainerGap(50, Short.MAX_VALUE)))
                    .addGroup(layout.createSequentialGroup()
                        .addGap(3, 3, 3)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jlbTongTienXuat, javax.swing.GroupLayout.PREFERRED_SIZE, 32, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addGroup(layout.createSequentialGroup()
                                .addGap(8, 8, 8)
                                .addComponent(jLabel3)))
                        .addContainerGap(48, Short.MAX_VALUE))))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void jbtThoatActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jbtThoatActionPerformed
        // TODO add your handling code here:
        this.dispose();
    }//GEN-LAST:event_jbtThoatActionPerformed

    private void formInternalFrameOpened(javax.swing.event.InternalFrameEvent evt) {//GEN-FIRST:event_formInternalFrameOpened
        // TODO add your handling code here:
        hienThiTatCaDoanhThu();
    }//GEN-LAST:event_formInternalFrameOpened

    private void jbtThongKeActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jbtThongKeActionPerformed
        // TODO add your handling code here:
        hienThiTatCaDoanhTheoThang();
    }//GEN-LAST:event_jbtThongKeActionPerformed

    private void jbtToanBoDoanhThuActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jbtToanBoDoanhThuActionPerformed
        // TODO add your handling code here:
        hienThiTatCaDoanhThu();
    }//GEN-LAST:event_jbtToanBoDoanhThuActionPerformed

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JButton jbtThoat;
    private javax.swing.JButton jbtThongKe;
    private javax.swing.JButton jbtToanBoDoanhThu;
    private com.toedter.calendar.JDateChooser jdcngaybatdau;
    private com.toedter.calendar.JDateChooser jdcngayketthuc;
    private javax.swing.JLabel jlbTongTienXuat;
    private javax.swing.JTable jtbPhieuXuatHang;
    // End of variables declaration//GEN-END:variables
}
